//
//  QBStatusCell.swift
//  Wei91BoiOS
//
//  Created by bing on 16/3/25.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit

import SnapKit

/// 自定义的微博cell
class QBStatusCell: UITableViewCell {

    // MARK: - 属性
    /// cell的BottomView的顶部约束
    var bottomViewBottomCons: Constraint?
    /// 微博模型
    var status: QBStatus? {
        didSet {
            // 当控制器把模型传给cell的时候调用
            // 将模型传递给CZStatusOriginalView
            originalView.status = status
            
            // 判断是否有被转的微博模型,有被转发的微博模型就显示被转发微博的view如果没有被转发的微博模型,就不显示被转发微博的view
            // 先不要这个设置
            bottomViewBottomCons?.uninstall()
            
            if let retweetStatus = status?.retweeted_status {
                // 有被转发的模型，显示被转发的view
                retweetView.hidden = false
                
                // 将这个被转发的模型赋值个被转发的View
                retweetView.retweetStatus = retweetStatus
                
                // 添加约束
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                // 没有被转发的模型，不显示被转发的view
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewBottomCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    /// 实现这个构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            // 当参照都一样的时候，可以简写
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            //make.height.equalTo(80)
        }
        
        // 被转发微博View
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        // 底部的view
        bottomView.snp_makeConstraints { (make) -> Void in
            
            self.bottomViewBottomCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            // contentView的左上角参照cell
            make.leading.top.trailing.equalTo(self)
            // contentView底部参照originalView
            //make.bottom.equalTo(originalView).offset(20)
            //ontentView底部参照bottomView
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    /// 原创微博的view
    private lazy var originalView: QBStatusOriginalView = QBStatusOriginalView()
    /// 底部视图
    private lazy var bottomView: QBStatusBottomView = QBStatusBottomView()

    /// 被转发微博的view
    private lazy var retweetView: QBStatusRetweetView = QBStatusRetweetView()
}
